package com.project.generation;

import com.project.database.DataBaseData;
import com.project.database.TableData;
import com.project.model.ColumnData;
import com.project.util.Config;
import com.project.util.IoUtil;
import com.project.util.Util;
import lombok.SneakyThrows;

import java.io.File;
import java.net.URI;
import java.util.List;
import java.util.Map;

public class WebAppGeneration {

    private Config config;
    private DataBaseData dataBaseData;
    private String webAppPath;

    public WebAppGeneration(Config config, DataBaseData dataBaseData) {
        this.config = config;
        this.dataBaseData = dataBaseData;
    }

    public void start() {
        this.catalogGeneration();
        this.templateGeneration();

        List<TableData> tables = this.dataBaseData.getTables();

        // 导航页面生成
        this.jspIndexGeneration(tables);

        // 列表以及添加页面生成
        this.jspTemplateGeneration(tables);

    }

    @SneakyThrows
    private void jspIndexGeneration(List<TableData> tables) {
        URI indexTemplatePath = this.getClass().getClassLoader()
                .getResource("template-webapp/template-index.jsp").toURI();
        File file = new File(indexTemplatePath);
        String indexTemplate = IoUtil.readFile(file);

        StringBuilder pathIndex = new StringBuilder();
        for (TableData table : tables) {
            pathIndex.append("              <li class=\"layui-nav-item\">\n" +
                    "                    <a class=\"\" href=\"javascript:;\" onclick=\"go('/page/" + table.getTableName()
                    + "/list')\">" + table.getTableName() + "</a>\n" +
                    "                </li>\n");
        }

        indexTemplate = indexTemplate.replaceAll(Util.getTemplateString("pathIndex"), pathIndex.toString());

        IoUtil.write(indexTemplate, this.webAppPath + "\\" + "WEB-INF\\global\\index.jsp");
    }

    private void jspTemplateGeneration(List<TableData> list) {
        for (TableData tableData : list) {
            this.listTemplateGeneration(tableData);
            this.addTemplateGeneration(tableData);
        }
    }

    @SneakyThrows
    private void listTemplateGeneration(TableData tableData) {
        URI listTemplatePath = this.getClass().getClassLoader()
                .getResource("template-webapp/template-list.jsp").toURI();
        File file = new File(listTemplatePath);
        String listTemplate = IoUtil.readFile(file);

        StringBuilder fields = new StringBuilder();
        StringBuilder tableFields = new StringBuilder();
        Map<String, ColumnData> tableFiled = tableData.getTableFiled();
        List<String> tableFiledList = tableData.getTableFiledList();
        for (String fn : tableFiledList) {
            ColumnData columnData = tableFiled.get(fn);
            fields.append("        <div class=\"layui-col-xs5\">\n" +
                    "            <div class=\"layui-form-item\">\n" +
                    "                <label class=\"layui-form-label\">"+columnData.getRemarks()+"</label>\n" +
                    "                <div class=\"layui-input-block\">\n" +
                    "                    <input type=\"text\" name=\""+fn+"\" required  lay-verify=\"required\" placeholder=\"请输入内容\" autocomplete=\"off\" class=\"layui-input\">\n" +
                    "                </div>\n" +
                    "            </div>\n" +
                    "        </div>\n");

            tableFields.append("                    , {field: '"+fn+"', title: '"+columnData.getRemarks()
                    +"', align: \"center\"}\n");
        }

        listTemplate = listTemplate.replaceAll(Util.getTemplateString("primaryKey"), tableData.getPrimaryKey());
        listTemplate = listTemplate.replaceAll(Util.getTemplateString("tableName"), tableData.getTableName());
        listTemplate = listTemplate.replaceAll(Util.getTemplateString("fields"), fields.toString());
        listTemplate = listTemplate.replaceAll(Util.getTemplateString("tableFields"), tableFields.toString());

        IoUtil.write(listTemplate, this.webAppPath+"\\"+"WEB-INF"+"\\"+tableData.getTableName()+"\\"+"list.jsp");
    }

    @SneakyThrows
    private void addTemplateGeneration(TableData tableData) {
        URI addTemplatePath = this.getClass().getClassLoader()
                .getResource("template-webapp/template-add.jsp").toURI();
        File file = new File(addTemplatePath);
        String addTemplate = IoUtil.readFile(file);

        StringBuilder fields = new StringBuilder();
        Map<String, ColumnData> tableFiled = tableData.getTableFiled();
        List<String> tableFiledList = tableData.getTableFiledList();
        for (String fn : tableFiledList) {
            ColumnData columnData = tableFiled.get(fn);
            fields.append("        <div class=\"layui-col-xs5\">\n" +
                    "            <div class=\"layui-form-item\">\n" +
                    "                <label class=\"layui-form-label\">"+columnData.getRemarks()+"</label>\n" +
                    "                <div class=\"layui-input-block\">\n" +
                    "                    <input type=\"text\" name=\""+fn+"\" required  lay-verify=\"required\" placeholder=\"请输入内容\" autocomplete=\"off\" class=\"layui-input\">\n" +
                    "                </div>\n" +
                    "            </div>\n" +
                    "        </div>\n");
        }

        addTemplate = addTemplate.replaceAll(Util.getTemplateString("tableName"), tableData.getTableName());
        addTemplate = addTemplate.replaceAll(Util.getTemplateString("fields"), fields.toString());

        IoUtil.write(addTemplate, this.webAppPath+"\\"+"WEB-INF"+"\\"+tableData.getTableName()+"\\"+"add.jsp");
    }


    @SneakyThrows
    private void templateGeneration() {
        URI templateURL = this.getClass().getClassLoader().getResource("template-webapp/template").toURI();
        File file = new File(templateURL);
        IoUtil.fileTransfer(file, this.webAppPath);
    }

    private void catalogGeneration() {
        this.webAppPath = config.getOutPath() + "\\" + config.getProjectName() + "\\" + config.getWebAppPath();
        File file = new File(this.webAppPath);
        if ( file.exists() ) file.delete();
        file.mkdirs();
    }

}
